Page({

  /**
   * 页面的初始数据
   */
  data: {
   address: {},
   cart: [],
   totalPrice: 0,
   totalNum: 0
  },
  //获取缓存中的收货地址信息
  onShow() {
    const address = wx.getStorageSync('address');
    //给data复制
    const cart = wx.getStorageSync('cart') || [];
    // 过滤购物车数组
    let checkedCart =  cart.filter(v => v.checked);
    this.setCartData(checkedCart); //<==并入该方法
    this.setData({
      address, //地址
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //获取地址的事件
  onaddress() {
    // 获取用户的收货地址
  wx.chooseAddress({
    success: (result) => {
      //将信息存入缓存
      wx.setStorageSync('address', result);
    }
   });
  },
  //购物车数据
  setCartData(cart) {
    let totalPrice = 0,
        totalNum = 0; 
    cart.forEach(v => {
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
    });
    this.setData({
      cart,  //购物车数组
      totalPrice,
      totalNum
    });
    //重新写入缓存
    wx.setStorageSync('cart', cart);
  },
  onPay() {
    wx.showModal({
      title: "支付",
      content: '完成支付',
      success: (res) => {
        if (res.confirm) {
            this.setData({
              cart: [],
              totalPrice: 0,
              totalNum: 0
            });
            this.setCartData([]);
            wx.navigateBack();
        }
      } 
    })
  }
});